package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import pos.domain.User;
import pos.utils.UIDGenerator;

public class JDBCUserDAO implements IUserDAO {
	private Connection conn;
	
	public JDBCUserDAO() {
		this.conn = ConnectionManager.getInstance().checkOut();
	}
	
	public void deleteUser(String userid) {
		String sql = "DELETE FROM users WHERE (userID = ?)";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, userid);
			stmt.executeUpdate();

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}
		}
	}

	@Override
	public void insertUser(User usuario) {
		String sql = "INSERT INTO users (oid, userID, name, subname, address, zipCode, city, provincy, tlf, email, usr, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		PreparedStatement stmt = null;
		String oidu = UIDGenerator.getInstance().getKey();
		
		try{
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, oidu);
			stmt.setString(2, usuario.getUserID());
			stmt.setString(3, usuario.getName());
			stmt.setString(4, usuario.getSubname());
			stmt.setString(5, usuario.getAddress());
			stmt.setString(6, usuario.getZipCode());
			stmt.setString(7, usuario.getCity());
			stmt.setString(8, usuario.getProvincy());
			stmt.setString(9, usuario.getTelefone());
			stmt.setString(10, usuario.getEmail());
			stmt.setString(11, usuario.getUser());
			stmt.setString(12, usuario.getPassword());
			
			stmt.executeUpdate();
		} catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}

	@Override
	public List<User> selectUser(String userid) {
		String sql = "SELECT * FROM users WHERE (userID = ?)";
		PreparedStatement stmt = null;
		ResultSet result = null;
		List<User> details = new LinkedList<User>();

		try{
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, userid);
			result = stmt.executeQuery();
			
			while (result.next()) {
				User u = new User();
				u.setUserID(result.getString("userID"));
				u.setAddress(result.getString("address"));
				u.setCity(result.getString("city"));
				u.setEmail(result.getString("email"));
				u.setName(result.getString("name"));
				u.setPassword(result.getString("password"));
				u.setProvincy(result.getString("provincy"));
				u.setSubname(result.getString("subname"));
				u.setTelefone(result.getString("tlf"));
				details.add(u);
			}

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}		
		return details;
	}

}
